package com.itcast.mallapplet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.admin.entity.User;
import com.itcast.applet.dto.ShoppingDhDTO;
import com.itcast.applet.entity.Commodity;
import com.itcast.applet.entity.Shopping;
import com.itcast.applet.entity.ShoppingDh;
import com.itcast.applet.entity.ShoppingPrice;
import com.itcast.applet.vo.ShoppingDhVO;
import com.itcast.applet.vo.ShoppingDityAll;
import com.itcast.context.BaseContext;
import com.itcast.mallapplet.mapper.CommodityMapper;
import com.itcast.mallapplet.mapper.ShoppingMapper;
import com.itcast.mallapplet.mapper.UserMapper;
import com.itcast.mallapplet.service.ShoppingService;
import com.itcast.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ShoppingServiceImpl implements ShoppingService {
    @Autowired
    private ShoppingMapper shoppingMapper;
    @Autowired
    private CommodityMapper commodityMapper;

    //根据商品id添加到购物车
    @Transactional
    public void addShopping(Long id) {
        //判断购物车分表里面是否有这个商品id
        ShoppingDh shopDh = shoppingMapper.getByDity(id);
        if (shopDh == null) {
            //获取商品的全部信息
            Commodity commodity = commodityMapper.getByCommodityId(id);
            if (commodity != null) {
                //找到user用户id所对应的购物车id
                Long userId = BaseContext.getCurrentId();
                Shopping shopping = shoppingMapper.getByShoppingId(userId);

                ShoppingDh shoppingDh = ShoppingDh.builder()
                        .shoppingId(shopping.getId())
                        .name(commodity.getName())
                        .number(1)
                        .price(commodity.getPrice())
                        .dityId(commodity.getId())
                        .build();
                //构建购物车分表
                shoppingMapper.insertshoppingDh(shoppingDh);
                //完善主购车车里面的所有数据
                shopping.setNumber(shopping.getNumber() + 1);
                shopping.setPrice(shopping.getPrice() + commodity.getPrice());
                shoppingMapper.amendShopping(shopping);
            }
        } else {
            //不等于null代表数据已经在购物车分表里面  1.两步mysql 只需新建一个mysql 加set
            ShoppingDh shopDh1 = shoppingMapper.getByDity(id);
            shopDh1.setNumber(shopDh1.getNumber() + 1);
            //修改购物车分表数据
            shoppingMapper.amendShoppingDh(shopDh1);
            //修改主购物车数据
            Shopping shopping = shoppingMapper.getByShoppingId(BaseContext.getCurrentId());
            shopping.setNumber(shopping.getNumber() + 1);
            shopping.setPrice(shopping.getPrice() + shopDh1.getPrice());
            shoppingMapper.amendShopping(shopping);
        }
    }

    //删除购物车商品
    @Transactional
    public void deleteShopping(List<Long> ids) {
        List<ShoppingPrice> shoppingDhList = shoppingMapper.seletShopping(ids);
        //通过批量删除商品的id找到所属数据 ----数据库直接查这些商品的价格和数量
        shoppingMapper.deleteShopping(ids);
        Double totalPrice = 0.0;
        int number = 0;
        for (ShoppingPrice shoppingDh : shoppingDhList) {
            //要删除的全部商品价格
            totalPrice += shoppingDh.getPrice() * shoppingDh.getNumber();
            number += shoppingDh.getNumber();
        }
        //获取购物车主表
        Shopping shopping = shoppingMapper.getByShoppingId(BaseContext.getCurrentId());
        shopping.setPrice(shopping.getPrice() - totalPrice);
        shopping.setNumber(shopping.getNumber() - number);
        //修改购物车主表
        shoppingMapper.amendShopping(shopping);
    }

    //修改购物车商品数量
    @Transactional
    public void amendShopping(ShoppingDhDTO shoppingDhDTO) {
        //通过id去找购物车分表里面的商品数据
        ShoppingDh shoppingDh = shoppingMapper.getById(shoppingDhDTO.getId());
        Long shoppingId = shoppingDh.getShoppingId();
        //修改购物车的数量不能大于商品的库存数量 --- 修改分表
        Commodity commodity = commodityMapper.getById(shoppingDh.getDityId());
        if (shoppingDh.getNumber() <= commodity.getNumber()) {
            shoppingDh.setNumber(shoppingDhDTO.getNumber());
            shoppingMapper.amendShoppingDh(shoppingDh);
            //修改完购物车分表然后修改其主表 数量和价格 --- 修改主表
            List<ShoppingDh> shoppingDhList = shoppingMapper.getByShoppingDhId(shoppingId);
            double totalPrice = 0.0;
            int number = 0;
            for (ShoppingDh dh : shoppingDhList) {
                totalPrice += dh.getPrice() * dh.getNumber();
                number += dh.getNumber();
            }
            Shopping shopping = shoppingMapper.getByShoppingId(BaseContext.getCurrentId());
            shopping.setNumber(number);
            shopping.setPrice(totalPrice);
            shoppingMapper.amendShopping(shopping);
        }
    }


    public PageResult queryAllShopping(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<ShoppingDhVO> Page = shoppingMapper.pageQueryShoppingAll(BaseContext.getCurrentId(), page, pageSize);
        List<ShoppingDhVO> result = Page.getResult();
        return new PageResult(Page.getTotal(), result);
    }
}
